﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Technoera.Common.Interop
{
    public class SimpleDataTypeDefinition : DataTypeDefinition
    {
        public SimpleDataTypeDefinition(SimpleDbType dbtype) : base(GetDbType(dbtype)) { }

        private static Dictionary<SimpleDbType, DbType> _conversionDictionary;
        private static Dictionary<SimpleDbType, DbType> GetConversionDictionary()
        {
            if (_conversionDictionary == null)
            {
                _conversionDictionary = new Dictionary<SimpleDbType, DbType>();
                _conversionDictionary.Add(SimpleDbType.Bit, DbType.Boolean);
                _conversionDictionary.Add(SimpleDbType.Byte, DbType.Byte);
                _conversionDictionary.Add(SimpleDbType.Int16, DbType.Int16);
                _conversionDictionary.Add(SimpleDbType.Int32, DbType.Int32);
                _conversionDictionary.Add(SimpleDbType.Int64, DbType.Int64);
                _conversionDictionary.Add(SimpleDbType.Date, DbType.Date);
                _conversionDictionary.Add(SimpleDbType.Time, DbType.Time);
                _conversionDictionary.Add(SimpleDbType.DateTime, DbType.DateTime);
                _conversionDictionary.Add(SimpleDbType.Single, DbType.Single);
                _conversionDictionary.Add(SimpleDbType.Double, DbType.Double);
                _conversionDictionary.Add(SimpleDbType.Currency, DbType.Currency);
                _conversionDictionary.Add(SimpleDbType.Guid, DbType.Guid);
            }
            return _conversionDictionary;
        }

        private static DbType GetDbType(SimpleDbType dbType)
        {
            return GetConversionDictionary()[dbType];
        }


        //private static DbType GetDbType(SimpleDbType dbType)
        //{
        //    DbType result = (DbType)dbType;
        //    if (!result.ToString().Equals(dbType.ToString()))
        //    {
        //        throw new ApplicationException("Framework version error: System.Data.SqlDbType has different values from .NET 2.0 version.");
        //    }
        //    return result;
        //}
    }
}
